package com.cuoyi.coupon.mall.common;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author zhang.cheng
 * 2022-02-28 17:16
 **/

public class ApiResult<T> implements Serializable {

    @ApiModelProperty(value = "业务数据")
    private T data;

    @ApiModelProperty(value = "状态码")
    private Integer resultCode;

    @ApiModelProperty(value = "错误信息")
    private String errMsg;

    public ApiResult() {
    }

    private ApiResult(Integer code, T data, String message) {
        this.resultCode = code;
        this.data = data;
        this.errMsg = message;
    }

    private ApiResult(Integer code, String message) {
        this.resultCode = code;
        this.errMsg = message;
    }

    private ApiResult(Integer code) {
        this.resultCode = code;
    }

    public static <T> ApiResult<T> ok() {
        return new ApiResult<>(200);
    }

    public static <T> ApiResult<T> ok(T data) {
        return new ApiResult<>(200, data, null);
    }

    public static <T> ApiResult<T> ok(T data, String message) {
        return new ApiResult<>(200, data, message);
    }

    public static <T> ApiResult<T> error() {
        return new ApiResult<>(500);
    }

    public static <T> ApiResult<T> error(String message) {
        return new ApiResult<>(500, message);
    }

    public static <T> ApiResult<T> error(Integer code, String message) {
        return new ApiResult<>(code, null, message);
    }

    public static <T> ApiResult<T> of(int code, T data, String message) {
        return new ApiResult<>(code, data, message);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getResultCode() {
        return resultCode;
    }

    public void setResultCode(Integer resultCode) {
        if (resultCode == null) {
            resultCode = 200;
        }
        this.resultCode = resultCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

}
